<!--
      scope 自定义展示上传文件样子
      limit 限制上传个数
      fileList 初始化上传文件列表
 -->

<template>
  <div class="upload">
    <el-upload
      class="upload-demo"
      action=""
      multiple
      :before-upload="beforeUpload"
      :http-request="handleRequest"
      :show-file-list="false"
      v-if="!uploadData.disabled"
      :list-type="uploadData.scope ? '' : 'picture-card'"
    >
      <template v-if="uploadData.scope">
        <slot></slot>
      </template>
      <template v-else>
        <i class="el-icon-plus"></i>
      </template>
    </el-upload>

    <el-row class="files-list" v-if="!uploadData.scope">
      <el-row class="item title">
        <el-col :span="20">文件名</el-col>
        <!-- <el-col :span="6">文件格式</el-col> -->
        <el-col :span="4" v-if="!uploadData.disabled">操作</el-col>
      </el-row>

      <el-row class="item" v-for="(item, index) in fileList" :key="index">
        <el-col :span="20" class="name">
          {{ item.originalFilename ? item.originalFilename : item.newFileName }}</el-col
        >
        <!-- <el-col :span="6">{{item.fileType}}</el-col> -->
        <el-col :span="4"
          ><i
            class="el-icon-delete-solid"
            @click="handleRemove(index)"
            v-if="!uploadData.disabled" />
          <i class="el-icon-view view" @click="handleReview(index)"
        /></el-col>
      </el-row>
    </el-row>
  </div>
</template>

<script>
import { uploads } from "@/api/common/index";
export default {
  data() {
    return {
      limit: this.uploadData.limit ? this.uploadData.limit : 10, //限制数量
      fileList: [], //返回图片数组
      tempList: [], //计数临时file
      files: [], //计数临时file
      visible: false,
    };
  },
  props: ["uploadData"],
  watch: {
    uploadData: {
      handler(news, old) {
        if (news) {
          if (news.list && news.list.length > 0) {
            this.fileList = news.list;
            this.backUpload();
          }
        }
      },
      immediate: true,
    },
  },
  mounted() {
    if (this.uploadData.list && this.uploadData.list.length > 0) {
      this.fileList = this.uploadData.list;
      this.backUpload();
    }
  },
  methods: {
    //上传之前
    beforeUpload(file) {
      this.tempList.push(file);
    },
    //上传成功
    handleRequest(res) {
      setTimeout(() => {
        this.files.push(res.file);
        const isLt2M = res.file.size / 1024 / 1024 < 10;
        if (!isLt2M) {
          this.MessageBox("提示", "上传文件 10MB!", "error");
          this.files = [];
          this.tempList = [];
          return;
        }
        if (this.files.length === this.tempList.length) {
          if (this.files.length + this.fileList.length <= this.limit) {
            let formFile = new FormData();
            for (let i = 0; i < this.files.length; i++) {
              formFile.append("files", this.files[i]);
            }
            uploads(formFile).then(response => {
              this.files = [];
              this.tempList = [];
              this.fileList = [...response.data, ...this.fileList];
              this.backUpload(this.fileList);
            });
          } else {
            this.files = [];
            this.tempList = [];
            this.MessageBox(
              "提示",
              `请选择文件，或最大上传数不超过${this.limit}张`,
              "error"
            );
          }
        }
      }, 200);
    },
    //回调给父级
    backUpload(fileList) {
      this.uploadData.onChange(this.uploadData.one ? fileList : this.fileList);
    },
    //移除内容
    handleRemove(key) {
      this.fileList.splice(key, 1);
    },
    //查看文件
    handleReview(key) {
      window.open(
        this.fileList[key].filePath
          ? this.fileList[key].filePath
          : this.fileList[key].url
      );
    },
  },
};
</script>

<style lang="scss">
.el-table {
  .upload,
  .upload-demo {
    display: inline-block !important;
  }
}

.el-upload--picture-card {
  width: 80px;
  height: 80px;
  line-height: 78px;
}
.files-list {
  width: 100%;
  display: inline-block;
  margin: 20px 0 0;
  .title {
    background: #f8f8f8;
  }
  .item {
    height: 40px;
    display: flex;
    align-items: center;
    position: relative;
    width: 100%;
    font-size: 12px;
    padding: 0 10px;
    .name {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    i {
      width: 10px;
      height: 100%;
      margin: 0 8px 0 0;
      cursor: pointer;
      font-size: 16px;
      color: #f78989;
    }
    .view {
      color: #3a8ee6;
    }
  }
}
</style>
